package com.example.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Account;
import com.example.entity.FriendApply;
import com.example.entity.FriendList;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.FriendApplyMapper;
import com.example.mapper.FriendListMapper;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 好友申请业务处理
 **/
@Service
public class FriendApplyService {

    @Resource
    private FriendApplyMapper friendApplyMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private FriendListMapper friendListMapper;

    /**
     * 新增
     */
    public void add(FriendApply friendApply) {
        Account currentUser = TokenUtils.getCurrentUser();
        friendApply.setUserId(currentUser.getId());
        friendApply.setCreateTime(DateUtil.now());
        friendApply.setStatus("申请中");
        FriendApply friendApply1 = friendApplyMapper.selectByUserIdAndApplyId(currentUser.getId(), friendApply.getApplyUserId());
        if (ObjectUtil.isNotEmpty(friendApply1)) {
            throw new CustomException("500", "你们已经是好友啦");
        }
        friendApplyMapper.insert(friendApply);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        friendApplyMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            friendApplyMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    @Transactional
    public void updateById(FriendApply friendApply) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (friendApply.getStatus().equals("已通过")) {
//            通过好友申请，创建好友信息
            FriendList friendList = new FriendList();
            FriendApply friendApply1 = friendApplyMapper.selectById(friendApply.getId());
            if (ObjectUtil.isNotEmpty(friendApply1)) {
//                创建属于当前用户的好友信息
                friendList.setUserId(currentUser.getId());
                friendList.setFriendId(friendApply1.getUserId());
//                判断是否已经有该好友信息
                List<FriendList> friendLists = friendListMapper.selectAll(friendList);
                if (CollectionUtil.isEmpty(friendLists)) {
                    friendList.setTime(DateUtil.now());
                    friendListMapper.insert(friendList);
                }

//                创建属于申请用户的好友信息
                FriendList friendList1 = new FriendList();
                friendList1.setUserId(friendApply1.getUserId());
                friendList1.setFriendId(currentUser.getId());
                List<FriendList> friendLists1 = friendListMapper.selectAll(friendList1);
                if (CollectionUtil.isEmpty(friendLists1)) {
                    friendList1.setTime(DateUtil.now());
                    friendListMapper.insert(friendList1);
                }
            }
        }
        friendApplyMapper.updateById(friendApply);
    }

    /**
     * 根据ID查询
     */
    public FriendApply selectById(Integer id) {
        return friendApplyMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<FriendApply> selectAll(FriendApply friendApply) {
        List<FriendApply> list = friendApplyMapper.selectAll(friendApply);
        for (FriendApply friendApply1 : list) {
            User user = userMapper.selectById(friendApply1.getApplyUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                friendApply1.setApplyUserName(user.getName());
            }
        }
        return list;
    }

    /**
     * 分页查询
     */
    public PageInfo<FriendApply> selectPage(FriendApply friendApply, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<FriendApply> list = this.selectAll(friendApply);
        return PageInfo.of(list);
    }

}